var express = require('express');
//var car = require('./model/car');
var news = require('./model/news');
var newsinfo = require('./model/newsInfo');
var comment = require('./model/comment');
var image = require('./model/images');
var imageinfo = require('./model/imageInfo');
var goods = require('./model/goods');
var gooddesc = require('./model/goodDesc')
var router = express.Router();


router.get('/get',function (req,res) {
    news.find(function (err,data) {
        if(err){
            return res.status(500).send('Server error.');
        }
        res.json({code:'0',news:data});
    })
});
router.get('/getnewinfo',function (req,res) {
    var id = req.query.id;
    console.log(id);
    newsinfo.findOne({id:id},function (err,data) {
        if(err){
            return res.status(500).send('Server error.');
        }
        res.json({code:'0',newsinfo:data});
    })
});
router.get('/getComment',function (req,res) {
    var id = req.query.id;
    var pageIndex = req.query.pageIndex;
    comment.find({
        id:id,
        pageIndex:pageIndex,
    },function (err,data) {
        if(err){
            return res.status(500).send('Server error.');
        }
        res.json({code:'0',comment:data});
    });

});

router.post('/addComment',function (req,res) {
    var id = req.body.id;
    var add_time = req.body.add_time;
    var content = req.body.content;
    var admin = new comment({
        id:id,
        user_name: "ywj",
        add_time:add_time,
        content: content,
        pageIndex: 1
    });
    admin.save(function (err, ret) {
        if (err) {
            console.log('保存失败')
        } else {
            res.json({code:'0',message:'添加成功'});
        }
    });
});

router.get('/getTitle',function (req,res) {
    res.json({code:'0',message:[
            {id:1,title:'体育'},
            {id:2,title:'民生'},
            {id:3,title:'经济'},
            {id:4,title:'教育'},
        ]})
});

router.get('/getImage',function (req,res) {
    var id = req.query.cateid;
    image.find({cateid:id},function (err,data) {
        if (err) {
            console.log('保存失败');
        } else {
            res.json({code:'0',images:data});
        }
    })
});
router.get('/getImageSrc',function (req,res) {
    var id = req.query.id;
    imageinfo.find({
        id:id,
    },function (err,data) {
        if (err) {
            console.log('保存失败');
        } else {
            res.json({code:'0',imageInfo:data[0]});
        }
    })
});

router.get('/getGoodsList',function (req,res) {
    goods.find(function (err,data) {
        if(err){
            console.log('发生错误');
        }else{
            res.json({code:'0',goods:data});
        }
    })
});
router.get('/getGood',function (req,res) {
    goods.findOne({
        id:req.query.id
    },function (err,data) {
        if(err){
            console.log('发生错误');
        }else{
            res.json({code:0,good:data});
        }

    })
});
router.get('/getGoodDesc',function (req,res) {
    gooddesc.findOne({
        id:req.query.id,
    },function (err,data) {
        if(err){
            console.log('发生错误');
        }else{
            res.json({code:0,gooddesc:data});
        }
    })
})


module.exports = router;